package com.itheima.tlias84.controller;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 客户端(Cookie)
 */
@RestController
@RequestMapping("cookie")
@Slf4j
public class TestCookieController {
    @GetMapping("save")
     ResultUtil saveCokkie(HttpServletResponse response) {

        Cookie cookie = new Cookie("username","liu");
        new Cookie("username","liu");
        response.addCookie(cookie);
        Cookie cookie1 = new Cookie("password", "123");
        response.addCookie(cookie1);
        return ResultUtil.success(cookie);

    }

    @GetMapping("/get")
    ResultUtil saveCookie(HttpServletRequest response) {
        Cookie[] cookies = response.getCookies();
        log.info("获取cookie:{}", cookies);
        JSONObject jsonObject = new JSONObject();
        for (Cookie cookie : cookies) {
            jsonObject.put(cookie.getName(), cookie.getValue());
            log.info("遍历cookie:{}", jsonObject);
        }
        return ResultUtil.success(jsonObject);
    }
}
